package cn.consult.netty;


import ch.qos.logback.core.joran.action.IncludeAction;
import cn.consult.common.utils.RedisUtils;
import cn.consult.sys.dao.*;
import cn.consult.sys.entity.TMeterReadingRecord;
import cn.consult.sys.entity.TWeEquipment;
import cn.consult.sys.entity.TWeGatewayConfig;
import cn.consult.sys.entity.TWeOperatingRecord;
import cn.consult.sys.vo.RuleVO;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 发送指令服务实现类
 */
@Service("sendInstructsService")
@Slf4j
public class SendInstructsServiceImpl implements SendInstructsService {

    @Resource
    private RedisUtils redisUtils;
    @Autowired
    private TWeGatewayConfigDao tWeGatewayConfigDao;
    @Autowired
    private SysDictDetailDao sysDictDetailDao;
    @Autowired
    private TWeEquipmentDao tWeEquipmentDao;
    @Autowired
    private TWeOperatingRecordDao tWeOperatingRecordDao;

    @Override
    public Object[] sendInstructs(String context, String ip, HttpServletRequest request, String deviceCode, String operationType) {
        log.info("prepare start send instructs0 {} {}", InstructTypeConstant.getByKey(operationType).getValue(), context);
        Long countTimeLong = 0L;
        try {
            a:
            while (true) {
                //防止一台设备同时下发多条指令，如果缓存中有记录则表示上次的操作还未完成
                String type = redisUtils.get(InstructConstant.handleInstructs + deviceCode);

                if (countTimeLong > 2000) {
                    log.info("send Instructs failed instructs: [{}]", context);
                    //超过20秒没有接到指令的返回，则会自动撤销上次操作
                    redisUtils.delete(InstructConstant.handleInstructs + deviceCode);
                    break;
                }
                log.info("wait return {}", type);
                Thread.sleep(500);
                countTimeLong = countTimeLong + 500;
                if (StringUtils.isEmpty(type)) {
                    redisUtils.set(InstructConstant.handleInstructs + deviceCode, operationType, 20);
                    byte[] b = ElectricityMeterUtil.fromHexString(context);
                    ChannelGroup channelGroup = BootNettyChannelInboundHandlerAdapter.getChannels();
                    Iterator channelIterator = channelGroup.iterator();
                    b:
                    while (channelIterator.hasNext()) {
                        Channel channel = (Channel) channelIterator.next();
                        InetSocketAddress insocket = (InetSocketAddress) channel.remoteAddress();
                        //给指定客户端ip发送消息
                        if (ip.equals(insocket.toString())) {
                            log.info("prepare start send instructs  {}", context);
                            channel.writeAndFlush(Unpooled.copiedBuffer(b));
                            channel.flush();
                            log.info(" send instructs end");
                            //发送接收等待结束
                            break a;
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return new Object[]{context, ip, request, deviceCode, operationType};
    }


    @Override
    public void callbackInstructResultMsg(String ip, String returnStr) {
        TWeOperatingRecord tWeOperatingRecord = new TWeOperatingRecord();
        log.info(returnStr);

        if (StringUtils.isEmpty(returnStr) || !returnStr.startsWith("FE")) {
            return;
        }
        //获取回调设备编号
        String notFEStr = returnStr.substring(returnStr.indexOf("68") + 2);
        String deviceType = notFEStr.startsWith("10") ? "w" : "e";
        //根据设备号找到数据，并更新数据
        if ("e".equals(deviceType)) {
            String addressNo = notFEStr.substring(0, notFEStr.indexOf("68"));
            //获取设备号
            String deviceNo = ElectricityMeterUtil.addressConvertNo(addressNo, true);
            String operationType = redisUtils.get(InstructConstant.handleInstructs + deviceNo);
            //删除redis中的数据
            redisUtils.delete(InstructConstant.handleInstructs + deviceNo);
            //电表操作
            handleOperation(returnStr, deviceNo, operationType);
            tWeOperatingRecord.setNumber(deviceNo);
            tWeOperatingRecord.setOperationType(InstructTypeConstant.getByKey(operationType).getValue());
        } else {
            String addressNo = notFEStr.substring(0, notFEStr.indexOf("811"));
            //获取设备号
            String deviceNo = ElectricityMeterUtil.addressConvertNo(addressNo, true);
            String operationType = redisUtils.get(InstructConstant.handleInstructs + deviceNo);
            //notFEStr     1092251510020000AA05A017000000BC16
            //获取设备号
            //水表操作
            String deviceCode = handleWaterOperation(notFEStr);
            tWeOperatingRecord.setNumber(deviceCode);
            tWeOperatingRecord.setOperationType(operationType);
            //删除redis中的数据
            redisUtils.delete(InstructConstant.handleInstructs + deviceCode);
        }
        Date date = new Date();
        //往操作记录插入数据  设备编号   操作用户  操作内容  操作ip  操作类型
        tWeOperatingRecord.setOperationIp(ip);
        tWeOperatingRecord.setOperationContent(returnStr);
        tWeOperatingRecord.setOperationTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        tWeOperatingRecord.setOperationUser("admin");
        tWeOperatingRecordDao.insert(tWeOperatingRecord);
    }

    /**
     * 处理水表返回操作
     *
     * @param notFEStr 返回指令内容
     */
    private String handleWaterOperation(String notFEStr) {
        String deviceNo = "";
        LambdaQueryWrapper<TWeEquipment> wrapper = new LambdaQueryWrapper<>();
        TWeEquipment vo = new TWeEquipment();
        if (notFEStr.indexOf("A0170000") >= 0) {
            //开阀成功
            //设备号
            deviceNo = notFEStr.substring(2, notFEStr.indexOf("AA05"));
            deviceNo = ElectricityMeterUtil.addressConvertNo(deviceNo, true);
            vo.setWaterValveStatus("已开阀");
        }
        if (notFEStr.indexOf("A0170001") >= 0) {
            //关阀成功
            deviceNo = notFEStr.substring(2, notFEStr.indexOf("AA05"));
            deviceNo = ElectricityMeterUtil.addressConvertNo(deviceNo, true);
            vo.setWaterValveStatus("已关阀");
        }
        if (notFEStr.indexOf("901F00") >= 0) {
            deviceNo = notFEStr.substring(2, notFEStr.indexOf("8116901F"));
            deviceNo = ElectricityMeterUtil.addressConvertNo(deviceNo, true);
            //查询水表
            String data = notFEStr.substring(notFEStr.indexOf("901F00") + 6, notFEStr.indexOf("901F") + 14);
            String dataStr = ElectricityMeterUtil.addressConvertNo(data, true);
            BigDecimal currWaterCount = new BigDecimal(dataStr).multiply(new BigDecimal("0.01"));
            log.info("water info {}", currWaterCount);
            handleWaterMater(deviceNo, vo, currWaterCount);

        }
        return deviceNo;

    }

    /**
     * 处理水表抄表信息
     *
     * @param deviceNo
     * @param vo
     * @param currWaterCount
     */
    @Transactional(rollbackFor = Exception.class)
    public void handleWaterMater(String deviceNo, TWeEquipment vo, BigDecimal currWaterCount) {
        //查询设备当前信息
        LambdaQueryWrapper<TWeEquipment> iotwrapper = new LambdaQueryWrapper<>();
        iotwrapper.eq(TWeEquipment::getEquipmentNumber, deviceNo);
        TWeEquipment devicevo = tWeEquipmentDao.selectOne(iotwrapper);

        BigDecimal bigDecimal = new BigDecimal(devicevo.getEquipmentResidueMoney());
        Integer equipmentResidueNumber = devicevo.getEquipmentResidueNumber();
        BigDecimal subtract = bigDecimal.subtract(currWaterCount);

        TMeterReadingRecord readingRecord = new TMeterReadingRecord();
        readingRecord.setNumber(deviceNo);
        readingRecord.setAddress(devicevo.getEquipmentAddress());
        readingRecord.setDeductMoney(Double.valueOf(String.valueOf(subtract)));

        int i1 = subtract.intValue();

        readingRecord.setDeductNumber(i1);
        readingRecord.setDeductBeforeMoney(devicevo.getEquipmentResidueMoney());
        readingRecord.setDeductBeforeNumber(equipmentResidueNumber);
        readingRecord.setRecordType("水表");
        readingRecord.setStatisticalTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        tMeterReadingRecordDao.insert(readingRecord);
    }


    /**
     * 电表处理操作类型，对不同的类型更新对应设备的信息
     *
     * @param deviceNo
     * @param type
     */
    private void handleOperation(String returnStr, String deviceNo, String type) {
        //deviceNo 设备编号  000000967933
        //type操作类型  4
        //notFEStr指令   FEFE683379960000006884025BF3E616
        LambdaQueryWrapper<TWeEquipment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TWeEquipment::getEquipmentNumber, deviceNo);
        //判断类型 然后修改状态
        TWeEquipment weStatus = new TWeEquipment();
        String opType = null;
        //合总电闸，拉闸或合闸后修改设备表状态
        if (InstructTypeConstant.E_MAIN_GATE_NO.getKey().equals(type)) {
            weStatus.setWaterMeterStatus("已合总闸");
            tWeEquipmentDao.update(weStatus, wrapper);
        }
        //拉总电闸
        if (InstructTypeConstant.E_MAIN_GATE_OFF.getKey().equals(type)) {
            weStatus.setWaterMeterStatus("已拉总闸");
            tWeEquipmentDao.update(weStatus, wrapper);
        }
        //合A路电闸
        if (InstructTypeConstant.E_A_GATE_NO.getKey().equals(type)) {
            weStatus.setEnergyMeterAstatus("已合A路电闸");
            tWeEquipmentDao.update(weStatus, wrapper);
        }
        //拉A路电闸
        if (InstructTypeConstant.E_A_GATE_OFF.getKey().equals(type)) {
            weStatus.setEnergyMeterAstatus("已拉A路电闸");
            tWeEquipmentDao.update(weStatus, wrapper);
        }
        //合B路电闸
        if (InstructTypeConstant.E_B_GATE_NO.getKey().equals(type)) {
            weStatus.setEnergyMeterBstatus("已合B路电闸");
            tWeEquipmentDao.update(weStatus, wrapper);
        }
        //拉B路电闸
        if (InstructTypeConstant.E_B_GATE_OFF.getKey().equals(type)) {
            weStatus.setEnergyMeterBstatus("已拉B路电闸");
            tWeEquipmentDao.update(weStatus, wrapper);
        }
        //获取电表参数信息
        if (InstructTypeConstant.E_MAIN_PARSM_INFO.getKey().equals(type)) {
            getElectricityParamInfo(returnStr, deviceNo);
        }
        //获取电表费控数据
        if (InstructTypeConstant.E_BALANCE_SETUPA.getKey().equals(type)) {
            getElectricityBalanceInfo(returnStr, deviceNo);
        }
    }


    @Autowired
    TMeterReadingRecordDao tMeterReadingRecordDao;

    /**
     * 定时任务抄表，回调此方法
     *
     * @param notFEStr
     * @param deviceCode
     */
    @Transactional(rollbackFor = Exception.class)
    public void getElectricityBalanceInfo(String notFEStr, String deviceCode) {
        //FEFE6833799600000068810643C3433333337B16
        //000000967933
        String dataStr = notFEStr.substring(notFEStr.indexOf("81104A1C") + 8, notFEStr.length() - 4);
        //购买次数
        String payCount = dataStr.substring(0, 4);
        String payCountStr = dataChange(payCount, 0);
        //最近购电金额
        String lastPayAmount = dataStr.substring(4, 12);
        String lastPayAmountStr = dataChange(lastPayAmount, 0);
        //剩余金额
        String surplusAmount = dataStr.substring(12, 20);
        surplusAmount = ElectricityMeterUtil.deviceAddressConvert(surplusAmount);
        surplusAmount = ElectricityMeterUtil.dataAreaSubtract33(surplusAmount);
        log.info("getElectricityBalanceInfo surplusAmount {} {}", surplusAmount);
        int amount = Integer.parseInt(surplusAmount.replaceAll(" ", ""), 16);
        log.info("getElectricityBalanceInfo amount {} {}", amount);
        String surplusAmountStr = String.valueOf(amount * 0.01);
        log.info("getElectricityBalanceInfo surplusAmount {} {}", surplusAmount, surplusAmountStr);
        //累计金额
        String sumAmount = dataStr.substring(20, 28);
        String sumAmountStr = dataChange(sumAmount, 0);
        log.info("getElectricityBalanceInfo surplusAmount {} {}", sumAmount, sumAmountStr);
        //查询设备信息

        LambdaQueryWrapper<TWeEquipment> tWeEquipmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tWeEquipmentLambdaQueryWrapper.eq(TWeEquipment::getEquipmentNumber, deviceCode);
        TWeEquipment tWeEquipment = tWeEquipmentDao.selectList(tWeEquipmentLambdaQueryWrapper).get(0);


        TMeterReadingRecord readingRecord = new TMeterReadingRecord();
        readingRecord.setNumber(deviceCode);
        readingRecord.setDeductBeforeMoney(tWeEquipment.getEquipmentResidueMoney());
        readingRecord.setDeductBeforeNumber(tWeEquipment.getEquipmentResidueNumber());
        readingRecord.setStatisticalTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        readingRecord.setRecordType("电表");
        readingRecord.setAddress(tWeEquipment.getEquipmentAddress());
        readingRecord.setDeductMoney(tWeEquipment.getEquipmentResidueMoney() - Double.valueOf(surplusAmountStr));
        //自己设备每度电1元钱
        double nu = Double.valueOf(surplusAmountStr) / 1;
        readingRecord.setDeductNumber(tWeEquipment.getEquipmentResidueNumber() - (int) nu);
        tMeterReadingRecordDao.insert(readingRecord);

        //改变设备表的数据  剩余金额  剩余电量

        Long id = tWeEquipment.getId();


    }


    /**
     * 获取电表参数信息
     *
     * @param str
     */
    private void getElectricityParamInfo(String str, String deviceCode) {
        //获取数据
        String datastr = str.substring(str.indexOf("B3E9") + 4, str.length() - 4);
        String dataBCD = ElectricityMeterUtil.hexAddcharacter(datastr, " ");
        log.info("getElectricityParamInfo subtract33Data {}", dataBCD);
        Map<String, String> map = new HashMap<>();

        //A路电压
        String aVoltage = datastr.substring(0, 4);
        aVoltage = dataChange(aVoltage, 0);
        log.info("A路电压：" + aVoltage);
        map.put("aVoltage", aVoltage);
        //B路电压
        String bVoltage = datastr.substring(4, 8);
        bVoltage = dataChange(bVoltage, 0);
        log.info("B路电压" + bVoltage);
        map.put("bVoltage", bVoltage);
        //A路电流
        String aCurrent = datastr.substring(8, 12);
        aCurrent = dataChange(aCurrent, 2);
        log.info("A路电流" + aCurrent);
        map.put("aCurrent", aCurrent);
        //B路电流
        String bCurrent = datastr.substring(12, 16);
        bCurrent = dataChange(bCurrent, 2);
        log.info("B路电流" + bCurrent);
        map.put("bCurrent", bCurrent);
        //总功率
        String sumPower = datastr.substring(16, 22);
        sumPower = dataChange(sumPower, 4);
        log.info("总功率:" + sumPower);
        map.put("sumPower", sumPower);
        //A功率
        String aPower = datastr.substring(22, 28);
        aPower = dataChange(aPower, 4);
        log.info("A功率:" + aPower);
        map.put("aPower", aPower);
        //B功率
        String bPower = datastr.substring(28, 34);
        bPower = dataChange(bPower, 4);
        log.info("B功率:" + bPower);
        map.put("bPower", bPower);
        //总视在功率
        String sumApparentPower = datastr.substring(34, 40);
        sumApparentPower = dataChange(sumApparentPower, 4);
        log.info("总视在功率:" + sumApparentPower);
        map.put("sumApparentPower", sumApparentPower);
        //A路视在功率
        String aApparentPower = datastr.substring(40, 46);
        aApparentPower = dataChange(aApparentPower, 4);
        log.info("A路视在功率:" + aApparentPower);
        map.put("aApparentPower", aApparentPower);
        //B路视在功率
        String bApparentPower = datastr.substring(46, 52);
        bApparentPower = dataChange(bApparentPower, 4);
        log.info("B路视在功率:" + bApparentPower);
        map.put("bApparentPower", bApparentPower);
        //A路功率因素
        String aPowerFactor = datastr.substring(52, 56);
        aPowerFactor = dataChange(aPowerFactor, 3);
        log.info("A路功率因素:" + aPowerFactor);
        map.put("aPowerFactor", aPowerFactor);
        //B路功率因素
        String bPowerFactor = datastr.substring(56, 60);
        bPowerFactor = dataChange(bPowerFactor, 3);
        log.info("B路功率因素:" + bPowerFactor);
        map.put("bPowerFactor", bPowerFactor);
        //电网频率
        String frequency = datastr.substring(60);
        frequency = dataChange(frequency, 2);
        log.info("电网频率:" + frequency);
        map.put("frequency", frequency);
        redisUtils.set(InstructConstant.SUMMARY_INSTRUCTS + deviceCode, JSON.toJSONString(map));
    }

    /**
     * 处理网关状态
     *
     * @param ip
     * @param str
     * @param flag
     */
    @Override
    public void handleGateWayStatus(String ip, String str, boolean flag) {
        //心跳监控
        LambdaQueryWrapper<TWeGatewayConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TWeGatewayConfig::getGatewayIp, ip);
        TWeGatewayConfig vo = new TWeGatewayConfig();
        vo.setGatewayStatus(flag ? 1 : 0);
        vo.setUpdateTime(new Date().toString());
        tWeGatewayConfigDao.update(vo, wrapper);
    }

    /**
     * 数据转换
     *
     * @param aVoltage       元数据
     * @param precisionIndex 几位小数
     * @return
     */
    private static String dataChange(String aVoltage, int precisionIndex) {
        String dataBCD = ElectricityMeterUtil.hexAddcharacter(aVoltage, " ");
        String subtract33Data = ElectricityMeterUtil.dataAreaSubtract33(dataBCD);
        log.info("dataChange {}", subtract33Data);
        String changeAfterStr = ElectricityMeterUtil.addressConvertNo(subtract33Data, false);
        Long changeAfterLong = Long.valueOf(changeAfterStr);
        if (precisionIndex == 1) {
            return String.valueOf(changeAfterLong * 0.1);
        }
        if (precisionIndex == 2) {
            return String.valueOf(changeAfterLong * 0.01);
        }
        if (precisionIndex == 3) {
            return String.valueOf(changeAfterLong * 0.001);
        }
        if (precisionIndex == 4) {
            return String.valueOf(changeAfterLong * 0.0001);
        }
        return String.valueOf(changeAfterLong);
    }


    public void sendIotMeterconfigInstructs(RuleVO ruleVO, HttpServletRequest request) throws Exception {
        TWeEquipment tWeEquipment = tWeEquipmentDao.selectById(ruleVO.getId());

        TWeGatewayConfig tWeGatewayConfig = tWeGatewayConfigDao.selectById(tWeEquipment.getEquipmentGateway());

        String address = ElectricityMeterUtil.deviceAddressConvert(tWeEquipment.getEquipmentNumber());

        String meterAPower = ruleVO.getAPower() + "0";//A路功率String addAfterPower = addDataLength(meterAPower);
        String addAfterPower = addDataLength(meterAPower);

        // 处理后的发送数据
        String sendAPowerData = ElectricityMeterUtil.deviceAddressConvert(addAfterPower);
        sendAPowerData = ElectricityMeterUtil.dataAreaPlus33(sendAPowerData);


        //获取发送指令
        //获取发送指令
        String instruce = ElectricityMeterUtil.getCompleteInstructSetUp(address, InstructConstant.ELECTRICITY_A_POWER_SETUP, sendAPowerData);
        //发送服务地址端口
        String newIp = "/" + tWeGatewayConfig.getGatewayIp() + ":" + tWeGatewayConfig.getGatewayPort();

        //下发A路最高使用功率配置
        sendInstructs(instruce, newIp, null, tWeEquipment.getEquipmentNumber(), InstructTypeConstant.E_A_POWER_SETUP.getKey());


        //下发A路周休特征字指令
        String aWeekInstruce = ElectricityMeterUtil.getCompleteInstruct(address, InstructConstant.ELECTRICITY_WEEY_A_SETUP);
        sendInstructs(aWeekInstruce, newIp, null, tWeEquipment.getEquipmentNumber(), InstructTypeConstant.E_A_WEEK_SETUP.getKey());
//下发B路周休特征字指
        String bWeekInstruce = ElectricityMeterUtil.getCompleteInstruct(address,
                InstructConstant.ELECTRICITY_WEEY_B_SETUP);
        sendInstructs(bWeekInstruce, newIp, null, tWeEquipment.getEquipmentNumber(), InstructTypeConstant.E_B_WEEK_SETUP.getKey());

        //A路工作日时段控制设置
        String a1 = getTimeStr(ruleVO.getATimeOne(), ruleVO.getATimeOneOperation());
        String a2 = getTimeStr(ruleVO.getATimeTwe(), ruleVO.getATimeTweOperation());
        String a3 = getTimeStr(ruleVO.getATimeThree(), ruleVO.getATimeThreeOperation());
        String a4 = getTimeStr(ruleVO.getATimeFour(), ruleVO.getATimeFourOperation());

        String aDataStr = a1 + "" + a2 + " " + a3 + " " + a4;
        String aPlush33Data = ElectricityMeterUtil.dataAreaPlus33(aDataStr);
        String alimeorkInstruce = ElectricityMeterUtil.getCompleteInstructSetUp(address, InstructConstant.ELECTRICITY_A_TIME_WORK_SETUP, aPlush33Data);
        sendInstructs(alimeorkInstruce, newIp, null, tWeEquipment.getEquipmentNumber(), InstructTypeConstant.E_A_TIME_WORK_SETUPA.getKey());


        //A路休息日时段控制设
        String aTimeRestInstruce = ElectricityMeterUtil.getCompleteInstructSetUp(address, InstructConstant.ELECTRICITY_A_TIME_REST_SETUP, aPlush33Data);
        sendInstructs(aTimeRestInstruce, newIp, null, tWeEquipment.getEquipmentNumber(), InstructTypeConstant.E_A_TIME_REST_SETUPA.getKey());

        //B路工作日时段控制设置
        String b1 = getTimeStr(ruleVO.getBTimeOne(), ruleVO.getBTimeOneOperation());
        String b2 = getTimeStr(ruleVO.getBTimeTwe(), ruleVO.getBTimeTweOperation());
        String b3 = getTimeStr(ruleVO.getBTimeThree(), ruleVO.getBTimeThreeOperation());
        String b4 = getTimeStr(ruleVO.getBTimeFour(), ruleVO.getBTimeFourOperation());

        String bDataStr = b1 + "" + b2 + " " + b3 + " " + b4;
        String bPlush33Data = ElectricityMeterUtil.dataAreaPlus33(bDataStr);
        String bTimeInstruce = ElectricityMeterUtil.getCompleteInstructSetUp(address, InstructConstant.ELECTRICITY_B_TIME_WORK_SETUP, bPlush33Data);
        sendInstructs(bTimeInstruce, newIp, null, tWeEquipment.getEquipmentNumber(), InstructTypeConstant.E_B_TIME_WORK_SETUPA.getKey());

        String bTimeRestInstruce = ElectricityMeterUtil.getCompleteInstructSetUp(address, InstructConstant.ELECTRICITY_B_TIME_REST_SETUP, bPlush33Data);
        sendInstructs(bTimeRestInstruce, newIp, null, tWeEquipment.getEquipmentNumber(), InstructTypeConstant.E_B_TIME_REST_SETUPA.getKey());
    }

    private String getTimeStr(String time, String status) {
        if (StringUtils.isEmpty(time) || StringUtils.isEmpty(status)) {
            return "11 00 00";
        }
        String repStr = time.replace(":", "");
        String istr = "";
        if ("1".equals(status)) {
            istr = "22";
        } else {
            istr = "11";
        }
        String constr = ElectricityMeterUtil.deviceAddressConvert(repStr);
        return istr + constr;
    }

    private String addDataLength(String str) {
        String s = "";
        if (str.length() < 6) {
            int num = 6 - str.length();
            for (int i = 0; i < num; i++) {
                s += "0";
            }
        }
        return s + str;
    }


}


